<script type="text/javascript">
var $datatable_export;
var _DATATABLE_TR;
var oTable_export;
var oTable_revert;
var FLAG_REVERT_CLICK = true;
var isREVERT = false;
<?php if( has_permission('KHVT.Product.Revert') ): ?>
    isREVERT = true;
<?php endif; ?>
    
$(document).ready(function() {
    $datatable_export = $('#datatable_export');
    $datatable_revert = $('#datatable_revert');
    $dialog_form_edit = $('#dialog_form_edit');
    $dialog_form_add = $('#dialog_form_add');
    $dialog_confrim_revert = $('#dialog_confrim_revert');
    
    oTable_export = $datatable_export.dataTable({
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "bInfo": false,
        "bProcessing": true,
        "bServerSide": true,
        "bPaginate": false,
        "bFilter" : false,
        "bLengthChange": false,
        //"bFilter": false,
        "sAjaxSource": $datatable_export.attr('active_url'),
        "aoColumns": [
          { "bSortable": false},
          { "bSortable": false},
          { "bSortable": false },
          { "bSortable": false },
          { "bSortable": false },
          { "bSortable": false },
          { "bSortable": false },
          { "bSortable": false },
          { "bSortable": false }
        ],
        "fnDrawCallback": function( oSettings ) {
            //$("#datatable_nb_wrapper").find(".fg-toolbar").removeClass("ui-widget-header");
            datatable_selected();
            _DATATABLE_TR = null;
        },
        "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
            if( aData['id'] != undefined ){
                $(nRow).attr("value_id",aData['id']);
            }
            if( aData['revert'] != undefined ){
                $(nRow).attr("revert",aData['revert']);
                if(aData['revert'] == 'no'){
                    $(nRow).addClass('noselected');
                }
                
            }
            $('td:eq(0)', nRow).html( iDisplayIndex+1 );
            return nRow;
        }
        //"oTableTools": {"aButtons": ["copy", "csv", "xls", "pdf"]}
        
    });
    
    oTable_revert = $datatable_revert.dataTable({
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "bInfo": false,
        "bProcessing": true,
        "bServerSide": true,
        "bPaginate": false,
        "bFilter" : false,
        "bLengthChange": false,
        //"bFilter": false,
        "sAjaxSource": $datatable_revert.attr('active_url'),
        "aoColumns": [
          { "bSortable": false},
          { "bSortable": false},
          { "bSortable": false },
          { "bSortable": false },
          { "bSortable": false },
          { "bSortable": false }
        ],
        //"fnDrawCallback": function( oSettings ) {
            //$("#datatable_nb_wrapper").find(".fg-toolbar").removeClass("ui-widget-header");
            //datatable_selected();
        //},
        "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
            $('td:eq(0)', nRow).html( iDisplayIndex+1 );
            return nRow;
        }
        //"oTableTools": {"aButtons": ["copy", "csv", "xls", "pdf"]}
        
    });
    
    dialogConfrimRevert();
    dialogError();
    
    $(function() {
        $( "#tabs" ).tabs();
    });
    
} );

function datatable_selected(){
    $datatable_export.find('tbody tr[revert=yes]').click( function( e ) {
        if ( $(this).hasClass('row_selected') ) {
            $(this).removeClass('row_selected');
            _DATATABLE_TR = null;
        }else {
            oTable_export.$('tr.row_selected').removeClass('row_selected');
            $(this).addClass('row_selected');
            _DATATABLE_TR = $(this).attr('value_id');
        }
    });
    
    $datatable_export.find('tbody tr[revert=yes]').bind("dblclick", function(){
        if ( !$(this).hasClass('row_selected') ) {
            $(this).addClass('row_selected');
        }
        _DATATABLE_TR = $(this).attr('value_id');
        //even_edit_dbclick( _DATATABLE_TR );
        even_revert_product();

    });

}

function even_edit_dbclick(id){
    var url = $dialog_form_edit.attr('active_url') + '/' + id;
        
    $.get(url, function(data) {
        $dialog_form_edit.html( data );
        $dialog_form_edit.dialog( "open" );
    });
}

function validate_frm(){
    var flag = true;
    var name = $('input[name=name]').val();
    if(name == ''){
        $("input[name=name]").addClass("i-error");
        $("#iname").show();
        flag = false;
    }else{
        $("#iname").hide();
        $("input[name=name]").removeClass("i-error");
    }
    
    return flag;
}

function dialogConfrimRevert(){
    $dialog_confrim_revert.dialog({
        autoOpen: false,
        //height: 300,
        width: 420,
        modal: true,
        resizable: true,
        buttons: {
            "<?php print lang('btn_save') ?>": function() {
                var $this = $( this );
                /*
                var $frm_revert = $dialog_confrim_revert.find('#frmRevert');
                var url = $frm_revert.attr('active_url');
                var revert = $dialog_confrim_revert.find('input[name=revert]').val();
                var export_total = $dialog_confrim_revert.find('input[name=export_total]').val();
                var revert_total = $dialog_confrim_revert.find('input[name=revert_total]').val();
                var flag = true;
                
                
                if(revert != ''){
                    revert = parseInt(revert);
                    export_total = parseInt(export_total);
                    revert_total = parseInt(revert_total);
                    
                    if( (revert + revert_total) > export_total || revert=='' || revert==0 ){
                        flag = false;
                    }
                }else{
                    flag = false;
                }

                if(flag==true){
                    $.post(url, {id:_DATATABLE_TR,revert:revert},function(data) {
                        if(data.flag == true){
                            $this.dialog( "close" );
                            alert(data.message);
                        }else{
                            $this.dialog( "close" );
                            alert(data.message);
                        }
                        oTable_export.fnDraw();
                        oTable_revert.fnDraw();
                    }, "json");
                    $dialog_confrim_revert.find('input[name=revert]').removeClass("i-error");
                }else{
                    $dialog_confrim_revert.find('input[name=revert]').addClass("i-error");
                    alert('gia tri khong phu hop');
                }
                */
                /*
                
                $("#frm_add").ajaxSubmit({
                    //beforeSubmit: validate
                    dataType:  'json',
                    beforeSubmit: function(formData, jqForm, options) {                        
                        return validate_frm();
                    },
                    success: function(data) {
                        if(data.action == 'update'){
                            var $tr = oTable_export.$('tr[value_id="'+ data.id +'"]');
                            $tr.find('td:eq(1)').html(data.name);
                            $tr.find('td:eq(2)').html(data.status);
                        }
                        $this.dialog( "close" );
                    }
                });
                */
                if(FLAG_REVERT_CLICK == true){
                    FLAG_REVERT_CLICK = false;
                    $dialog_confrim_revert.find('.dialog-content').fadeOut(200,function(){
                        $dialog_confrim_revert.find('.dialog-loading').fadeIn(200,function(){
                            var url = $dialog_confrim_revert.find('#frmRevert').attr('active_url');
                            var revert = $dialog_confrim_revert.find('input[name=revert]').val();
                            $.post(url, {id:_DATATABLE_TR,revert:revert},function(data) {                                
                                if(data.flag == true){
                                    $this.dialog( "close" );
                                    oTable_export.fnDraw();
                                    oTable_revert.fnDraw();
                                    $dialog_confrim_revert.find('.dialog-loading').hide();
                                    $dialog_confrim_revert.find('.dialog-content').html('').show();
                                    FLAG_REVERT_CLICK = true;
                                    alert('<?php print lang('vt_message_001'); ?>');
                                }else{
                                    $("input.i-error").removeClass("i-error");
                                    $("select.i-error").removeClass("i-error");
                                    $("div.error").hide();
                                    $.each(data.error, function(key, value) {
                                        $("input[name="+value.name+"]").addClass("i-error");
                                        $("select[name="+value.name+"]").addClass("i-error");
                                        $("#error_"+value.name).html(value.message);
                                        $("#error_"+value.name).show();
                                    });
                                    $dialog_confrim_revert.find('.dialog-loading').delay(100).fadeOut(200,function(){
                                        $dialog_confrim_revert.find('.dialog-content').fadeIn(200);
                                        FLAG_REVERT_CLICK = true;
                                    });
                                }
                            }, "json");
                        });
                    });
                }
            },
            "<?php print lang('btn_cancel') ?>": function() {
                $( this ).dialog( "close" );
            }
        },
        close: function() {
            $dialog_form_edit.find('.dialog-content').html('');
            FLAG_REVERT_CLICK = true;
        }
    });
        
}

function even_revert_product(){
    if(isREVERT == true){
        if(_DATATABLE_TR != null){
            $('#content-loading').height($('#content-warpp').outerHeight());
            $('#content-loading').find('img').css({'top':'50%','left':'50%','margin-top':'-100px','margin-left':'-100px'});
            $('#content-loading').fadeIn(200,function(){
                var url = $dialog_confrim_revert.attr('active_url');
                $.post(url, {id:_DATATABLE_TR},function(data) {
                    $dialog_confrim_revert.find('.dialog-content').html( data );
                    $dialog_confrim_revert.dialog( "open" );
                    $('#content-loading').delay(300).fadeOut(200,function(){
                        //$('#product-list').fadeIn(200);
                        //FLAG_WARP_LEFT_CLICK = true;
                    });
                });
            });
            
        /*var url = $dialog_confrim_revert.attr('active_url');
        $.post(url, {id:_DATATABLE_TR},function(data) {
            $dialog_confrim_revert.html( data );
            $dialog_confrim_revert.dialog( "open" );
        });*/
        }else{
            $dialog_error.html( '<?php print lang('error_noselected'); ?>' );
            $dialog_error.dialog( "open" );
        }
    }
}



</script>

<div class="warpp">
    <div>
        <table>
            <tbody>
                <tr>
                    <td>
                        <table>
                            <tbody>
                                <tr>
                                    <td><label><?php print lang('vt_nguoinhap'); ?>:</label></td>
                                    <td><?php print $bill->username; ?></td>
                                </tr>
                                <tr>
                                    <td><label><?php print lang('vt_maphieuxuat'); ?>:</label></td>
                                    <td><input readonly="readonly" name="bill_code" value="<?php print $bill->bill_code; ?>" class="itext ivalidate ui-widget-content ui-corner-all"></td>
                                </tr>
                                <tr>
                                    <td><label><?php print lang('vt_choncongtrinh'); ?>:</label></td>
                                    <td>
                                        <select name="supplier_id" class="itext ivalidate ui-widget-content ui-corner-all">
                                            <option><?php print $bill->name; ?></option>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td><label><?php print lang('vt_nguoinhanphieu'); ?>:</label></td>
                                    <td>
                                        <select name="supplier_id" class="itext ivalidate ui-widget-content ui-corner-all">
                                            <option><?php print $bill->name; ?></option>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td><label><?php print lang('vt_total-vat'); ?>:</label></td>
                                    <td><input readonly="readonly" name="bill_total" value="<?php print decimal_format($bill->bill_total); ?>" class="itext ivalidate ui-widget-content ui-corner-all"></td>
                                </tr>
                                <tr>
                                    <td><label><?php print lang('vt_vat%'); ?>:</label></td>
                                    <td><input readonly="readonly" name="bill_vat" value="<?php print decimal_format($bill->bill_vat); ?>" class="itext ivalidate ui-widget-content ui-corner-all"></td>
                                </tr>
                                <tr>
                                    <td><label><?php print lang('vt_vatprice'); ?>:</label></td>
                                    <td><input readonly="readonly" name="bill_vatprice" value="<?php print decimal_format($bill->bill_vatprice); ?>" class="itext ivalidate ui-widget-content ui-corner-all"></td>
                                </tr>
                                
                            </tbody>
                        </table>
                    </td>
                    
                    <td>
                        <table>
                            <tr>
                                <td><label><?php print lang('vt_note'); ?>:</label></td>
                            </tr>
                            <tr>
                                <td>
                                    <textarea readonly="readonly" name="bill_note" style="width: 380px;" cols="40" rows="3"><?php print $bill->bill_note; ?></textarea>
                                </td>
                            </tr>
                            <tr>
                                <td><label><?php print lang('vt_total+vat'); ?>:</label>: <input readonly="readonly" name="bill_totalvat" value="<?php print decimal_format($bill->bill_totalvat); ?>" class="itext ivalidate ui-widget-content ui-corner-all"></td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </tbody>
        </table>
                        
    </div>

    <div id="tabs">
        <ul>
            <li><a href="#tabs-1"><?php print lang('vt_tab_productexportbill'); ?></a></li>
            <li><a href="#tabs-2"><?php print lang('vt_tab_proexpbillrevert'); ?></a></li>
        </ul>
        <div id="tabs-1" style="position: relative;">
            <div id="content-loading" style="display: none;">
                <img class="icon_loading_2" src="<?php print base_url('template/ess/images/loading/loading_3.gif') ?>" alt="loading...">
            </div>
            <div id="content-warpp">
                <div class="menu-button">
                    <div class="message"></div>
                    <?php if( has_permission('KHVT.Product.Revert') ): ?>
                    <button onclick="even_revert_product();"><?php print lang('btn_travattu'); ?></button>
                    <?php endif; ?>
                    <div class="clear"></div>
                </div>
                <table cellpadding="0" cellspacing="0" border="0" class="display" id="datatable_export" active_url="<?php print site_url('xk/datatable_bill/'.$bill->id); ?>">
                    <thead>
                        <tr>
                            <th width="40px"><?php print lang('vt_stt'); ?></th>
                            <th width="100px"><?php print lang('vt_mavt'); ?></th>
                            <th width="200px"><?php print lang('vt_tenvt'); ?></th>
                            <th><?php print lang('vt_donvitinh'); ?></th>
                            <th><?php print lang('vt_soluongxuat'); ?></th>
                            <th><?php print lang('vt_dongia'); ?></th>
                            <th><?php print lang('vt_thanhtien'); ?></th>
                            <th><?php print lang('vt_note'); ?></th>
                            <th><?php print lang('vt_soluongtra'); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td colspan="5" class="dataTables_empty">Loading data from server</td>
                        </tr>
                    </tbody>
                </table>
                <div class="clear"></div>
            </div>
        </div>
        <div id="tabs-2">
            <table cellpadding="0" cellspacing="0" border="0" class="display" id="datatable_revert" active_url="<?php print site_url('xk/datatable_revert/'.$bill->id); ?>">
                <thead>
                    <tr>
                        <th><?php print lang('vt_stt'); ?></th>
                        <th><?php print lang('vt_mavt'); ?></th>
                        <th><?php print lang('vt_tenvt'); ?></th>
                        <th><?php print lang('vt_donvitinh'); ?></th>
                        <th><?php print lang('vt_soluongtra'); ?></th>
                        <th><?php print lang('vt_ngaytra'); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td colspan="5" class="dataTables_empty">Loading data from server</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

<div id="dialog_confrim_revert" title="<?php print lang('vt_tab_proexpbillrevert'); ?>" active_url="<?php print site_url('xk/ajax_loadfrm_revert/'); ?>">
    <div class="dialog-loading" style="display: none;">
        <img class="icon_loading_3" src="<?php print base_url('template/ess/images/loading/loading_5.gif') ?>" alt="loading...">
    </div>
    <div class="dialog-content"></div>
</div>